package com.edu.app.config;

import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler;
import com.edu.app.bean.core.SysTenant;
import com.edu.app.threadlocal.TenantContext;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;

public class TenantDBHandler implements TenantHandler {


        @Override
        public Expression getTenantId() {
            //HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

            //return new LongValue(1L);
             SysTenant tenant = (SysTenant) TenantContext.getCurrentTenant();
             if(tenant == null)
                 return new LongValue(0L);
            return  new LongValue(tenant.getId());
        }

        @Override
        public String getTenantIdColumn() {
            return "tenant_id";
        }

        @Override
        public boolean doTableFilter(String tableName) {
            // 这里可以判断是否过滤表
            if(TenantContext.getCurrentTenant() == null){
                return true;
            }
            if(!TenantContext.getNeedTenantId()){
                return true;
            }

           if ("s_tenant".equalsIgnoreCase(tableName)) {
                return true;
            }
            return false;
        }
}
